package com.wemei.auto.ui

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Process
import androidx.lifecycle.ProcessLifecycleOwner
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.wemei.auto.ui.helper.ProcessLifecycleObserver
import com.wemei.auto.ui.utils.ActivityManger

object AppCore {
    lateinit var app: Application

    @JvmStatic
    fun init(application: Application) {
        app = application

        val processName = getProcessName(app)
        if (app.packageName == processName) {
            init()
        }
    }

    private fun init() {
//        Logger.isDebug = BuildConfig.DEBUG
//        Album.initialize(
//            AlbumConfig.newBuilder(this)
//                .setAlbumLoader(GlideAlbumLoader()) // 设置Album加载器。
//                .setLocale(Locale.CHINA) // 比如强制设置在任何语言下都用中文显示。
//                .build()
//        )
        ActivityManger.registerActivityLifecycleCallbacks(app)
        ProcessLifecycleOwner.get().lifecycle.addObserver(ProcessLifecycleObserver)
    }

    private fun getProcessName(context: Context): String? {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val runningApps = am.runningAppProcesses ?: return null
        for (proInfo in runningApps) {
            if (proInfo.pid == Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName
                }
            }
        }
        return null
    }

    init {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.white, R.color.text_color) //全局设置主题颜色
            ClassicsHeader(context)
        }
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.white, R.color.text_color) //全局设置主题颜色
            ClassicsFooter(context).setDrawableSize(20f)
        }
    }
}